package org.example.mppg.domain.entity;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.github.javafaker.Faker;
import lombok.Data;
import org.apache.ibatis.type.ArrayTypeHandler;
import org.example.mppg.handler.JsonbTypeHandler;
import org.example.mppg.util.DateUtil;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * <p>
 * 教师信息表
 * </p>
 *
 * @author bo
 * @since 2024-10-14
 */
@TableName("teacher")
@Data
public class Teacher implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 姓名
     */
    @TableField("tea_name")
    private String teaName;

    /**
     * 教学年级
     */
    @TableField("grade_no")
    private Integer gradeNo;

    /**
     * 教学班级
     */
    @TableField("class_no")
    private Integer classNo;

    /**
     * 额外信息,jsonb类型
     */
    @TableField(value = "info", typeHandler = JsonbTypeHandler.class)
    private JSONObject info;

    /**
     * 数组
     */
    @TableField(typeHandler = ArrayTypeHandler.class)
    private Integer[] codes;


    @TableField(typeHandler = JacksonTypeHandler.class)
    private Object jsonStr;

    /**
     * 创建时间，默认为当前时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间，默认为当前时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 是否删除标志，默认为0（未删除）
     */
    @TableField("is_deleted")
    @TableLogic
    private Integer isDeleted;


    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", teaName=" + teaName +
                ", gradeNo=" + gradeNo +
                ", classNo=" + classNo +
                ", info=" + info +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", isDeleted=" + isDeleted +
                "}";
    }

    public static List<Teacher> GenerateTeachers(int num) {
        return IntStream.range(0, num)
                .mapToObj(i -> Teacher.generate())
                .collect(Collectors.toList());
    }

    public static Teacher generate() {
        Teacher teacher = new Teacher();
        Faker faker = new Faker(Locale.CHINA);
        Random random = new Random();

        teacher.setTeaName(faker.name().fullName());
        teacher.setGradeNo(random.nextInt(12) + 1);
        teacher.setClassNo(random.nextInt(10) + 1);
        JSONObject jsonObject = generateInfo(faker);
        teacher.setInfo(jsonObject);
        teacher.setCodes(new Integer[]{1, 2, 3});

        teacher.setJsonStr(jsonObject);
        teacher.setCreateTime(DateUtil.randomLocalDateTime("2010-10-11", "2024-10-11"));
        teacher.setUpdateTime(DateUtil.randomLocalDateTime("2010-10-11", "2024-10-11"));
        return teacher;
    }

    private static JSONObject generateInfo(Faker faker) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        boolean includeAddress = random.nextBoolean();
        boolean includeAge = random.nextBoolean();

        JSONObject jsonObject = new JSONObject();

        if (includeAddress) {
            jsonObject.put("address", faker.address().fullAddress());
        }
        if (includeAge) {
            jsonObject.put("age", faker.number().numberBetween(6, 18));
        }

        return jsonObject;
    }
}
